package com.uairobot.bill.websocket.chat.web.interceptors;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import com.uairobot.bill.websocket.chat.web.constants.ParamKey;
@Component
public class MyWebSocketHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

  public MyWebSocketHandshakeInterceptor() {
    super.setCopyHttpSessionId(false);
  }

  @Override
  public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
      Map<String, Object> attributes) throws Exception {
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    String userId = servletRequest.getParameter(ParamKey.USERID);
    attributes.put(ParamKey.USERID, userId);
    return super.beforeHandshake(request, response, wsHandler, attributes);
  }

  @Override
  public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
      Exception ex) {
    super.afterHandshake(request, response, wsHandler, ex);
  }
}